#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include "graphicsview.h"
#include <QProcess>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    scene(new Touche::Scene(0., 0., 800., 424.))
{
    ui->setupUi(this);
    Touche::GraphicsView *w = new Touche::GraphicsView(scene);
    w->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    w->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    w->setFixedSize(800, 424);
    w->setRenderHint(QPainter::Antialiasing);
    QMainWindow::setCentralWidget(w);
}

MainWindow::~MainWindow()
{
    delete ui;
    QProcess().execute("/usr/sbin/dsmetool -t /usr/bin/camera-ui");
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::on_actionSaveAs_triggered()
{
    scene->saveStage(fileName = QFileDialog::getSaveFileName(this,  "Choose a file to save the document", QDir::homePath(), QString::fromUtf8("Touché files (*.tchz)")));
}

void MainWindow::on_actionSave_triggered()
{
    if(fileName.isEmpty())
        fileName = QFileDialog::getSaveFileName(this, "Choose a file to save the document", QDir::homePath(), QString::fromUtf8("Touché files (*.tchz)"));
    if(!fileName.isEmpty())
        scene->saveStage(fileName);
}

void MainWindow::on_actionNew_triggered()
{
    if(scene->isModified()) {
        int ret = QMessageBox::warning(this, trUtf8("Touché"),
                                       tr("Do you want to save the changes in the current document?"),
                                       QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,
                                       QMessageBox::Cancel);

        switch(ret)
        {
        case QMessageBox::Save:
            on_actionSave_triggered();
            break;
        case QMessageBox::Discard:
            break;
        case QMessageBox::Cancel:
        default:
            return;
        }
    }

    scene->newStage();
}

void MainWindow::on_actionOpen_triggered()
{
    scene->loadStage(QFileDialog::getOpenFileName(this, "Choose a document to open", QDir::homePath(), QString::fromUtf8("Touché files (*.tchz)")));
}

void MainWindow::closeEvent(QCloseEvent *e)
{
    QMainWindow::closeEvent(e);

    if(!scene->isModified()) {
        e->accept();
        return;
    }

    switch(QMessageBox::warning(this, trUtf8("Touché"),
                                tr("Do you want to save the changes?"),
                                QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,
                                QMessageBox::Cancel))
    {
    case QMessageBox::Save:
        if(!save()) {
            e->ignore();
            return;
        }
    case QMessageBox::Discard:
        e->accept();
        break;
    case QMessageBox::Cancel:
    default:
        e->ignore();
        return;
    }

    scene->deleteLater();
}

bool MainWindow::save()
{
    if(fileName.isEmpty())
        fileName = QFileDialog::getSaveFileName(this, "Choose a file to save the document", QDir::homePath(), QString::fromUtf8("Touché files (*.tchz)"));

    if(!fileName.isEmpty()) {
        return scene->saveStage(fileName);
    } else {
        return false;
    }
}
